<template>
  <div>
    <el-dialog title="编辑商品" :visible.sync="dialogVisible" width="70%" :before-close="clearForm" style="margin-left: 80px">
      <!-- 内容区域 -->
      <el-form :model="goodsForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品id" prop="id">
          <el-input v-model="goodsForm.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品名称" prop="title">
          <el-input v-model="goodsForm.title" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model.number="goodsForm.price" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="num">
          <el-input v-model.number="goodsForm.num" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="发布时间" prop="date">
          <el-date-picker type="datetime" placeholder="选择日期" v-model="goodsForm.date" style="width: 100%" value-format="yyyy-MM-dd HH:mm:ss"> </el-date-picker>
        </el-form-item>
        <el-form-item label="商品卖点" prop="sellPoint">
          <el-input v-model="goodsForm.sellPoint" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品图片" prop="image">
          <div class="img-btn">
            <el-button type="primary" @click="innerVisibleImg = true" :disabled="true">上传图片</el-button>
            <img class="img" :src="goodsForm.image" style="margin-left: 10px" alt="" />
          </div>
        </el-form-item>
        <el-form-item label="商品描述" prop="descs">
          <WangEditor ref="myEditor" @sendEditor="sendEditor" />
        </el-form-item>
      </el-form>

      <!-- 弹框底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="clearForm">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>

      <!-- 2. 内弹框 --- 上传图片 -->
      <el-dialog width="40%" title="上传图片" :visible.sync="innerVisibleImg" append-to-body>
        <UploadImg @sendImg="sendImg" ref="upload" />
        <span slot="footer" class="dialog-footer">
          <el-button @click="innerVisibleImg = false">取 消</el-button>
          <el-button type="primary" @click="showImg">确 定</el-button>
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import UploadImg from '../../components/Uploadimg.vue'
import WangEditor from '../../components/WangEditor.vue'
export default {
  name: 'FirstProjectEditGoods',
  components: {
    UploadImg,
    WangEditor
  },
  props: {
    rowData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      dialogVisible: false, //外弹框
      innerVisible: false, //内弹框
      innerVisibleImg: false, //图片弹框
      imgUrl: '',
      goodsForm: {
        id: '',
        title: '',
        price: '',
        num: '',
        sellPoint: '',
        image: '',
        descs: '',
        date: ''
      },
      rules: {
        // title: [
        //   { required: true, message: '请输入商品名称', trigger: 'blur' },
        //   { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        // ],
        // price: [
        //   { required: true, message: '请输入价格', trigger: 'blur' },
        //   { type: 'number', message: '价格必须为数字值' }
        // ],
        // num: [
        //   { required: true, message: '请输入数量', trigger: 'blur' },
        //   { type: 'number', message: '数量必须为数字值' }
        // ]
      }
    }
  },
  mounted() {},
  // 监听器
  watch: {
    rowData(val) {
      console.log(val)
      this.$nextTick(() => {
        this.$refs.myEditor.editor.txt.html(val.descs)
      })
    }
  },

  methods: {
    // 接收editor值
    sendEditor(val) {
      this.goodsForm.descs = val
    },
    sendImg(val) {
      console.log('显示图片的地址', val)
      this.imgUrl = val
    },
    showImg() {
      this.innerVisibleImg = false
      this.goodsForm.image = this.imgUrl
      //清空图片上传的列表
      this.$refs.upload.fileList = []
    },
    submitForm() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          //   let { id, title, price, num, sellPoint, image, descs, date } = this.goodsForm
          //   date = new Date(date).getTime()
          //   date = this.formatDateTime(date)
          //   descs = this.$refs.myEditor.text
          //   const { data: res } = await this.$http.post('/goods/edit', { id, title, price, num, sellPoint, image, descs, date })
          //   if (res.code !== 0) {
          //     return this.$message.error('修改失败')
          //   }
          //   this.$message.success('修改成功')
          //   this.clearForm()
          //   this.$refs.ruleForm.resetFields()
          //   this.$parent.getData()
          //   this.$parent.getPageTotal()
          console.log(valid)
          this.dialogVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    formatDateTime(value) {
      // 时间戳转换日期格式方法
      if (value == null) {
        return ''
      } else {
        const date = new Date(value)
        const y = date.getFullYear() // 年
        let MM = date.getMonth() + 1 // 月
        MM = MM < 10 ? '0' + MM : MM
        let d = date.getDate() // 日
        d = d < 10 ? '0' + d : d
        let h = date.getHours() // 时
        h = h < 10 ? '0' + h : h
        let m = date.getMinutes() // 分
        m = m < 10 ? '0' + m : m
        let s = date.getSeconds() // 秒
        s = s < 10 ? '0' + s : s
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
      }
    },
    clearForm() {
      this.dialogVisible = false

      //   this.goodsForm = {
      //     title: '',
      //     price: '',
      //     num: '',
      //     sellPoint: '',
      //     image: '',
      //     descs: '',
      //     date: ''
      //   }
      //   this.$refs.myEditor.editor.txt.clear()
    }
  }
}
</script>

<style lang="scss" scoped>
.line {
  text-align: center;
}
.img {
  width: 120px;
  height: 120px;
  margin-left: 20px;
}
.img-btn {
  width: 280px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
::v-deep .el-input__inner {
  color: green !important;
}
</style>
